package com.yjht.online.generate.impl.person;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.yjht.online.arg.ArgParam;
import com.yjht.online.generate.Generate;
import com.yjht.online.generate.GlobalContext;
import com.yjht.online.generate.impl.person.plat.kd.PersonGenerateProssorKd;
import com.yjht.online.generate.impl.person.plat.wyx.PersonGenerateProssorWyx;
import com.yjht.online.generate.model.org.LrdOrg;
import com.yjht.online.generate.model.person.PersonModel;
import com.yjht.online.generate.model.person.PersonModelExcel;
import com.yjht.online.generate.person.PersonGenerateProssor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PersonGenerate implements Generate {

    final static Logger logger = LoggerFactory.getLogger(PersonGenerate.class);

    @Override
    public void generate(ArgParam argParam) {
        if (StrUtil.isBlank(argParam.getPersonFilePath())) {
            return;
        }
        if (!FileUtil.exist(argParam.getPersonFilePath())) {
            logger.info("人员信息指定文件不存在");
            return;
        }

        InputStream inputStream = FileUtil.getInputStream(argParam.getPersonFilePath());
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        List<PersonModelExcel> result = null;
        try {
            result = ExcelImportUtil.importExcel(inputStream, PersonModelExcel.class, params);
        } catch (Exception exception) {
            logger.error("人员信息Excel读取失败" + exception.getMessage());
        }
        if (result == null) {
            logger.error("未读取到人员信息");
        }
        result = result
                .stream()
                .filter(f -> StrUtil.isNotBlank(f.getIndex()) && StrUtil.isNotBlank(f.getLoginAcoount()))
                .collect(Collectors.toList());

        if (CollectionUtil.isEmpty(result)) {
            logger.error("未读取到人员信息");
        }

        List<PersonModel> personModels = toPersonModel(result);
        logger.info("生成机构人员");
        PersonGenerateProssor personGenerateProssorKd = new PersonGenerateProssorKd();
        PersonGenerateProssor personGenerateProssorWyx = new PersonGenerateProssorWyx();
        personGenerateProssorKd.generatePerson(personModels);
        logger.info("快贷生成机构人员完成");
        personGenerateProssorWyx.generatePerson(personModels);
        logger.info("微营销生成机构人员完成");
        logger.info("生成机构人员完成");
    }

    private List<PersonModel> toPersonModel(List<PersonModelExcel> personModelExcels) {

        List<PersonModel> personModels = new ArrayList<>();
        String corpCd = "";

        if (GlobalContext.getCorpInfo() != null && StrUtil.isNotBlank(GlobalContext.getCorpInfo().getCorpCd())) {
            corpCd = GlobalContext.getCorpInfo().getCorpCd();
        }

        for (PersonModelExcel personModelExcel : personModelExcels) {
            PersonModel personModel = new PersonModel();
            BeanUtil.copyProperties(personModelExcel, personModel);
            //机构号
            String orgCd = deductionOrgCd(personModelExcel.getOrgName(), personModelExcel.getDepartment());
            personModel.setOrgCd(orgCd);
            personModel.setCorpCd(corpCd);
            personModels.add(personModel);
        }

        return personModels;
    }

    /**
     * 推断机构号 先匹配  orgName 后再匹配 deparment
     *
     * @param orgName
     * @return
     */
    private String deductionOrgCd(String orgName, String deparment) {
        if (StrUtil.isBlank(orgName)) {
            return "";
        }
        List<LrdOrg> lrdOrgs = GlobalContext.getLrdOrgs();
        if (CollectionUtil.isEmpty(lrdOrgs)) {
            return "";
        }

        //根据orgName
        for (LrdOrg lrdOrg : lrdOrgs) {
            if (StrUtil.isBlank(lrdOrg.getOrgName())) {
                continue;
            }
            if (lrdOrg.getOrgName().contains(orgName)) {
                return lrdOrg.getOrgCd();
            }
        }

        //根据deparment
        for (LrdOrg lrdOrg : lrdOrgs) {
            if (StrUtil.isBlank(lrdOrg.getOrgName())) {
                continue;
            }
            if (lrdOrg.getOrgName().contains(deparment)) {
                return lrdOrg.getOrgCd();
            }
        }
        String shortOrgName = StrUtil.subAfter(orgName, "村镇银行", true);
        if (StrUtil.isBlank(shortOrgName)) {
            return StrUtil.isNotBlank(orgName) ? orgName : deparment;
        }
        for (LrdOrg lrdOrg : lrdOrgs) {
            if (StrUtil.isBlank(lrdOrg.getOrgName())) {
                continue;
            }
            if (lrdOrg.getOrgName().contains(shortOrgName)) {
                return lrdOrg.getOrgCd();
            }
        }
        String deparmentOrgName = StrUtil.subAfter(deparment, "村镇银行", true);
        if (StrUtil.isBlank(deparmentOrgName)) {
            return StrUtil.isNotBlank(orgName) ? orgName : deparment;
        }
        for (LrdOrg lrdOrg : lrdOrgs) {
            if (StrUtil.isBlank(lrdOrg.getOrgName())) {
                continue;
            }
            if (lrdOrg.getOrgName().contains(deparmentOrgName)) {
                return lrdOrg.getOrgCd();
            }
        }

        return StrUtil.isNotBlank(orgName) ? orgName : deparment;

    }
}
